package com.example.chao.myphotosearch.api;


import com.example.chao.myphotosearch.PhotoSearchApp;
import com.example.chao.myphotosearch.SimpleCache.ACache;
import com.example.chao.myphotosearch.bean.ImageJoy;
import com.example.chao.myphotosearch.bean.RecommendList;
import com.example.chao.myphotosearch.bean.SosoImage;
import com.example.chao.myphotosearch.bean.SosoImgResult;
import com.example.chao.myphotosearch.dao.Recommend;
import com.example.chao.myphotosearch.utils.NetworkUtil;
import com.example.chao.myphotosearch.widget.UIToast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by chao on 2016/3/29.
 */
public class HttpRetrofit {
    private static ACache aCache = ACache.get(PhotoSearchApp.getInstance());
    private static final String RECOMMEND_MODEL_CACHE_NAME = "recommendList";

    /**
     * 获取推荐页
     *
     * @param start
     * @param end
     * @param subscriber
     */
    public static void getRecommendModel(int start, int end, Subscriber<List<Recommend>> subscriber) {
        Observable<RecommendList> observable = null;
        //如果没网的时候读取缓存
        if (!NetworkUtil.isNetworkConnected()) {
            UIToast.showToastShort("没网还想调戏窝！");
            RecommendList recommendList = (RecommendList) aCache.getAsObject(RECOMMEND_MODEL_CACHE_NAME);
            if (recommendList != null) {
                observable = Observable.just(recommendList);
            }
        } else {
            //读取网端
            observable = ServiceClient.getApiService().getHeadList(start, end).doOnNext(new Action1<RecommendList>() {
                @Override
                public void call(RecommendList recommendList) {
                    if (recommendList != null || !recommendList.getRecommends().isEmpty()) {
                        //离线缓存
//                            DbService.getInstance().saveRecommendList(recommendList.getRecommends());
                        aCache.put(RECOMMEND_MODEL_CACHE_NAME, recommendList,aCache.TIME_DAY);
                    }
                }
            });
        }
        if(observable!=null) {
            observable.subscribeOn(Schedulers.io())
                    .map(new Func1<RecommendList, List<Recommend>>() {
                        @Override
                        public List<Recommend> call(RecommendList recommendList) {
                            return recommendList.getRecommends();
                        }
                    }).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(subscriber);
        }
    }

    /**
     * 获取图片
     *
     * @param word
     * @param page
     * @param subscriber
     */
    public static void getImageList(String word, int page, Subscriber<List<SosoImage>> subscriber) {
        if (!NetworkUtil.isNetworkConnected()) {
            UIToast.showToastShort("没网还想调戏窝！");
            return;
        }
        ServiceClient.getApiService().getImageList("ajax", "result", word, page * 24).map(new Func1<SosoImgResult, List<SosoImage>>() {
            @Override
            public List<SosoImage> call(SosoImgResult sosoImgResult) {
                return sosoImgResult.getItems();
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public static void getImageJoys(Subscriber<ArrayList<ImageJoy>> subscriber){
       ServiceClient.getApiService().getImageJoy()
                .map(new Func1<ImageJoy[], ArrayList<ImageJoy>>() {
                    @Override
                    public ArrayList<ImageJoy> call(ImageJoy[] imageJoys) {
                        return new ArrayList<ImageJoy>(Arrays.asList(imageJoys));
                    }
                }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);

    }
}
